package com.rfid.assistant.activity;

import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.google.gson.Gson;
import com.rfid.DirectoryBean;
import com.rfid.sockethelper.IOHelper;
import com.rfid.sockethelper.ServerSocketHelper;
import com.rfid.udp.UdpManager;
import com.rfid.util.LogUtils;
import com.rfid.wlandirectlib.WifiDirectManager;
import com.rfid.wlandirectlib.WifiGroupListener;

public class ServerActivity extends SRBaseActivity{


    public static void startActivity(Context context) {
        Intent intent = new Intent();
        intent.setClass(context,ServerActivity.class);
        context.startActivity(intent);
    }

    ServerSocketHelper helper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},101);
        helper = new ServerSocketHelper(this);
        UdpManager.getInstance().startBroadcastSelf("lw");
        int ret = WifiDirectManager.getInstance().initP2p(this);
        if (ret != 0) {
            Toast.makeText(this, "wifi直连初始化失败：" + ret, Toast.LENGTH_SHORT);
        }
        LogUtils.v("init ret = " + ret);
//        WifiDirectManager.getInstance().startDiscovery();
        if (ret == 0) {
            WifiDirectManager.getInstance().setGoupListener(new WifiGroupListener() {
                @Override
                public void onCreateGroup(boolean isSuccess) {
                    LogUtils.v("onCreateGroup " + isSuccess);
                }

                @Override
                public void onRemoveGroup(boolean isSuccess) {
                    LogUtils.v("onRemoveGroup " + isSuccess);
                }
            });
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    WifiDirectManager.getInstance().createGroup();
                }
            },200);
        }
    }

    public void send(View view) {
        if (helper != null) {
            String text = mEditText.getText().toString();
            System.out.println("send " + text);
            if (!TextUtils.isEmpty(text)) {
                helper.send(text);
                mMessages.add(0,"发送：" + text);
                mAdapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    public void onConnect(String ip, boolean connect) {
        Toast.makeText(this,"ip > " + connect,Toast.LENGTH_SHORT).show();
        //服务端发文件，只发最后一个连接的
        if (connect) {
            mIp = ip;
        }
    }

    @Override
    protected void handleSelectedFile(Uri uri) {
        super.handleSelectedFile(uri);
        helper.sendFile(this,mIp,uri);
    }

    @Override
    protected void deleteRemoteFile(String path) {
        helper.sendDeleteFile(path);
    }

    @Override
    protected void requestFile(String path) {
        helper.sendReqFileCmd(mIp,path);
    }

    @Override
    public void onReceived(String ip, short cmd, CharSequence... text) {
        super.onReceived(ip, cmd, text);
        if (cmd == IOHelper.CMD_REQ_FILE_LIST) {
        } else if (cmd == IOHelper.CMD_REQ_FILE) {
        } else if (cmd == IOHelper.CMD_SEND_FILE_LIST) {
            Gson gson = new Gson();
            DirectoryBean bean = gson.fromJson(text[0].toString(),DirectoryBean.class);
        } else if (cmd == IOHelper.CMD_SET_FILE) {
            String path = text[0].toString();
        } else if (cmd == IOHelper.CMD_SEND_FILE) {
            String remotePath = text[0].toString();
        }
    }

    @Override
    protected void requestDir(String path) {
        helper.sendQueryFileListCmd(mIp,path);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        UdpManager.getInstance().destroy();
        helper.clear();
        WifiDirectManager.getInstance().removeGroup();
        WifiDirectManager.getInstance().destroy();
    }
}